const std = @import("std");
const jetzig = @import("jetzig");
const article_service = @import("../service/article_service.zig");

/// 文章详情页
pub fn get(id: []const u8, req: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
    const int_id = try std.fmt.parseInt(u32, id, 10);
    const article = try article_service.getArticleById(int_id);

    if (article) |art| {
        var root = try data.object();

        var art_obj = try data.object();
        try art_obj.put("id", data.integer(art.id));
        try art_obj.put("title", data.string(art.title));
        try art_obj.put("description", data.string(art.description));
        try art_obj.put("content", data.string(art.content));
        try art_obj.put("istop", data.boolean(art.istop == 1));
        try art_obj.put("cate_name", data.string(art.cate_name));
        try art_obj.put("created_at", data.string(art.created_at));
        try root.put("article", art_obj);

        return req.render(.ok);
    }

    return req.render(.not_found);
}
